layui.use(['layer','form','table','element'],function () {
    var layer = layui.layer
        ,form = layui.form
        ,table =layui.table
        ,element = layui.element;

    //动态加载下拉框
    $.ajax({
        type:"GET",
        url:"/admin/project/part/type/query?page=1&limit=1000",
        success:function (result) {
            var typeList = result.data;

            $.each(typeList,function (i,type) {
                var description = type.description;
                var id = type.id;
                $("#type").append("<option value='"+id+"'>"+description+"</option>");
            })

            //重新渲染下拉框
            form.render("select");
        },
        error:function () {
            layer.msg("系统错误！");
        }
    });

    //初始化数据表格
     table.render({
         elem:"#data"
         ,height: 'full-150'
         ,url:"/admin/project/part/query"
         ,cellMinWidth: 80
         ,cols: [[
                 {field:'id', width:100,title: 'ID'}
                 ,{field:'typeName', width:100,title: '类型', align:'center'}
                 ,{field:'content', width:600,title: '内容', align:'center'}
                 ,{field:'used', width: 100, title: '是否已使用',toolbar: '#titleBar'}
                 ,{field:'',width:280,title: '操作',toolbar: '#barDemo'}
             ]]
         ,page:true
     });

    //监听工具条
    table.on('tool(dataTable)',function (obj) {
        var data = obj.data;
        if(obj.event == 'del'){
            $.ajax({
                type:"DELETE"
                ,url:"/admin/project/part/"+data.id
                ,success:function (result) {
                    layer.msg(result.message);
                    table.reload("data");
                }
                ,error:function () {
                    layer.msg("系统错误");
                }
            });
        }else if(obj.event == 'edit'){
            window.open("/admin/project/part/editView?id="+data.id, "_blank");
        }else {
            //编辑项目
            layer.msg("操作错误！");
        }
    });


    function rightMenu(){

        $(".layui-tab-title li",window.parent.document).on("contextmenu",function (e) {
            var rightmenu = $(".rightmenu",window.parent.document);
            rightmenu.find("li").attr('data-id',$(this).attr("lay-id"));
            l = e.clientX;
            t = e.clientY;
            rightmenu.css({ left:l, top:t}).show();
            return false;
        });
        //左键点击隐藏
        $("body,.layui-tab-title li",window.parent.document).click(function(){
            $(".rightmenu").hide();
        });

    }

    function iframeWH(){
        var H = $(window).height();
        $("iframe",window.parent.document).css("height",H+"px");
    }
     
     //搜索
    $("#search").on("click",function () {
        table.reload("data",{
            page:{
                curr:1
            }
            ,where:{
                typeId:$("#type").val()
            }
        })
    });;

    $("#add").on("click",function () {

        parent.addTab($(this));

        // parent.layui.element.tabAdd('tabs',{
        //     title:"免费项目新增",
        //     content:"<iframe  src='/admin/project/freeToAdd'  frameborder='0'  style='width:100%;height: 100%'></iframe>",
        //     id:"free_project_add"
        // });
        // rightMenu();
        // iframeWH();
        // parent.layui.element.tabChange('tabs','free_project_add');


    });
});

function toAdd() {
    window.open("/admin/project/part/addView", "_blank");
}
function toCreate() {
    window.open("/admin/project/part/createView", "_blank");
}
